import 'package:flutter/material.dart';

class StatePage extends StatefulWidget {
  const StatePage({Key? key}) : super(key: key);

  @override
  _StatePageState createState() {
    print('StatePage createState.');
    return _StatePageState();
  }
}

class _StatePageState extends State<StatePage> {
  int _num = 0;

  @override
  void initState() {
    // 初始化时调用
    super.initState();
    print('StatePage initState.');
    _num = 1;
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          ElevatedButton(
            onPressed: _decrement,
            child: Text("-"),
          ),
          Padding(
            padding: EdgeInsets.all(20),
            child: Text('$_num'),
          ),
          ElevatedButton(
            onPressed: _increment,
            child: Text("+"),
          )
        ],
      ),
    );
  }

  @override
  void didUpdateWidget(StatePage oldWidget) {
    // 组件更新
    super.didUpdateWidget(oldWidget);
    print('StatePage didUpdateWidget.');
  }

  @override
  void deactivate() {
    // 组件不活跃
    super.deactivate();
    print('StatePage deactivate.');
  }

  @override
  void dispose() {
    // 组件销毁
    super.dispose();
    print('StatePage dispose.');
  }

  @override
  void didChangeDependencies() {
    // 在initState后，或InheritedWidget发生变更后调用
    super.didChangeDependencies();
    print('StatePage didChangeDependencies.');
  }

  void _increment() {
    setState(() {
      print('StatePage setState.');
      _num++;
    });
  }

  void _decrement() {
    setState(() {
      print('StatePage setState.');
      _num--;
    });
  }
}
